package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.model.domain.Announcement;
import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.vo.*;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.service.*;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author げっしょく
 * @date 2021/10/30
 */
@Service
public class MessagesService {

    @DubboReference
    private UserApi userApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private FriendApi friendApi;

    @DubboReference
    private CommentApi commentApi;

    @DubboReference
    private AnnouncementApi announcementApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    public UserInfoVo getUserInfo(String huanxinId) {
        User user = userApi.getUser(huanxinId);

        UserInfo userInfo = userInfoApi.getUserById(user.getId());
        UserInfoVo vo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo, vo);
        if (userInfo.getAge() != null) {
            vo.setAge(userInfo.getAge().toString());
        }

        return vo;
    }

    //添加好友
    public void addFriend(Long userId) {
        //获得操作用户
        Long id = UserHolder.getId();
        //在环信中互相添加为好友
        Boolean aBoolean = huanXinTemplate.addContact(Constants.HX_USER_PREFIX + id, Constants.HX_USER_PREFIX + userId);
        //判断是否添加成功
        if (!aBoolean) {
            throw new BusinessException(ErrorResult.error());
        }
        friendApi.addFriend(userId, id);
    }

    //删除好友
    public void deleteFriend(Long userId) {
        Long id = UserHolder.getId();
        Boolean aBoolean = huanXinTemplate.deleteContact(Constants.HX_USER_PREFIX + id, Constants.HX_USER_PREFIX + userId);
        if (!aBoolean) {
            throw new BusinessException(ErrorResult.error());
        }
        friendApi.deletFriend(userId, id);
    }

    public PageResult getFriends(Integer page, Integer pagesize, String keyword) {
        Long userId = UserHolder.getId();
        //从好友列表中查找对应的好友id
        List<Friend> friends =  friendApi.getFriendsId(page, pagesize, userId);
        //判断好友id列表是否为空
        if (CollUtil.isEmpty(friends)) {
            return new PageResult();
        }
        //通过好友id去访问用户详情表
        List<Long> friendIds = CollUtil.getFieldValues(friends, "friendId", Long.class);
        //通过好友id去访问用户详情表并找出符合条件的
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(keyword);
        Map<Long, UserInfo> map = userInfoApi.getByUserIds(friendIds, userInfo);
        List<ContactVo> vos = new ArrayList<>();
        //通过好友id找出对应的好友, 改变里面的id为环信内分配的id
        for (Friend friend : friends) {
            UserInfo userInfo1 = map.get(friend.getFriendId());
            if (userInfo1 != null) {
                ContactVo vo = ContactVo.init(userInfo1);
                vos.add(vo);
            }
        }
        PageResult pageResult = new PageResult(page, pagesize, 0L, vos);
        return pageResult;

    }

    //查询喜欢列表
    public PageResult subject(Integer page, Integer pagesize, CommentType commentType) {
        //通过当前用户id查找喜欢的人
        Long id = UserHolder.getId();
        List<Comment> list = commentApi.getSubject(page, pagesize, id, commentType);
        //判断互动消息是否为空
        if (CollUtil.isEmpty(list)) {
            return new PageResult();
        }
        //把互动人的id取出来
        List<Long> userIds = CollUtil.getFieldValues(list, "userId", Long.class);
        //使用互动人的id去查找用户详情表
        Map<Long, UserInfo> map = userInfoApi.getByUserIds(userIds, null);
        //封装成vo
        List<SubjectVo> vos = new ArrayList<>();
        for (Comment comment : list) {
            UserInfo userInfo = map.get(comment.getUserId());
            if (userInfo != null) {
                SubjectVo init = SubjectVo.init(userInfo, comment);
                vos.add(init);
            }
        }

        return new PageResult(page, pagesize, 0L, vos);
    }

    //查询评论列表
    public PageResult getNotice(Integer page, Integer pagesize) {
        List<Announcement> list = announcementApi.slectAll(page, pagesize);
        if (list == null) {
            return new PageResult();
        }
        List<NoticeVo> vos = new ArrayList<>();
        for (Announcement announcement : list) {
            NoticeVo init = NoticeVo.init(announcement);
            vos.add(init);
        }
        return new PageResult(page, pagesize, 0L, vos);
    }
}
